﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace EasyLibrary.AR.Classes
{
    public class Entrega:ActiveRecord
    {
        public int IDEntrega { get; set; }
        public RotaViatura Rv { get; set; }
        public double Latitude { get; set; }
        public double Longitude { get; set; }
        public RotaMarca Rm { get; set; }
        public DateTime Data { get; set; }
        public DateTime DataInsercao { get; set; }
        public bool Activo { get; set; }
        public string Funcionario { get; set; }

        public Entrega()
        {
            IDEntrega = -1;
            Rv=new RotaViatura();
            Latitude = -1;
            Longitude = -1;
            Rm=new RotaMarca();
          //  Data=new DateTime(0,0,0,0,0,0);
         //   DataInsercao=new DateTime(0,0,0,0,0,0);
            Activo = true;
            Funcionario = "";
        }
        public Entrega(int id,RotaViatura rotaViatura,double lat,double lng,RotaMarca rotaMarca,DateTime d,DateTime datain,bool act,string func)
        {
            this.IDEntrega = id;
            this.Rv = rotaViatura;
            this.Latitude = lat;
            this.Longitude = lng;
            this.Rm = rotaMarca;
            this.Data = d;
            this.DataInsercao = datain;
            this.Activo = act;
            this.Funcionario = func;
        }

        public Entrega(RotaViatura rotaViatura, double lat, double lng, RotaMarca rotaMarca, DateTime d, DateTime datain, bool act, string func)
        {
           // this.IDEntrega = id;
            this.Rv = rotaViatura;
            this.Latitude = lat;
            this.Longitude = lng;
            this.Rm = rotaMarca;
            this.Data = d;
            this.DataInsercao = datain;
            this.Activo = act;
            this.Funcionario = func;
        }

        public Entrega(DataRow row)
        {
            this.IDEntrega = Convert.ToInt32(row["IDEntrega"]);
            this.Rv.IdRotaViatura = (int) row["IdRotaViatura"];
            this.Latitude = (double) row["Latitude"];
            this.Longitude = (double)row["Longitude"];
            this.Rm.IDRotaMarca = (int)row["IDRotaMarca"];
            this.Data = (DateTime)row["Data"];
            this.DataInsercao = (DateTime)row["DataInsercao"];
            this.Activo = (bool)row["Activo"];
            this.Funcionario = (string)row["Funcionario"];
        }

        public int InserirEntrega(Entrega ent)
        {
            int ret=-1;

            string query = "Insert into entrega (IDRotaViatura,Latitude,Longitude,IDMarca,Data,Funcionario) values(@1,@2,@3,@4,@5,@6) ";
            /*INSERT INTO table_name (column1,column2,column3,...)
VALUES (value1,value2,value3,...)*/
            
            SqlCommand sqlcomm=new SqlCommand(query);
            sqlcomm.Parameters.AddWithValue("@1", ent.Rv.IdRotaViatura);
            sqlcomm.Parameters.AddWithValue("@2", ent.Latitude);
            sqlcomm.Parameters.AddWithValue("@3", ent.Longitude);
            sqlcomm.Parameters.AddWithValue("@4", ent.Rm.IDRotaMarca);
            sqlcomm.Parameters.AddWithValue("@5", ent.Data);
            sqlcomm.Parameters.AddWithValue("@6", ent.Funcionario);

            sqlcomm.CommandText = query;
           
            ret=ExecuteNonQuery(CurrentTransaction, sqlcomm);
            return ret;
        }

        public override void Save()
        {
            throw new NotImplementedException();
        }
    }
}
